package pt.ua.helpdesk.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class extends a composite and will allow the
 * user to edit an account. It must contain a space
 * to introduce a new password and its confirmation
 * 
 * @author Amigos do Noddy
 */
public class LoginAccount extends Composite {

	/**
	 * Class constructor.
	 * 
	 * Creates the widgets of the window
	 */
	public LoginAccount() {

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);

		final Label lblPass3 = new Label("Palavra-Chave Antiga:");
		horizontalPanel_1.add(lblPass3);
		lblPass3.setStyleName("label");

		final PasswordTextBox txtPass3 = new PasswordTextBox();
		horizontalPanel_1.add(txtPass3);
		txtPass3.setWidth("250");
		txtPass3.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_4);
		horizontalPanel_4.setSpacing(2);
		horizontalPanel_4.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		final Label lblPass1 = new Label("Nova Palavra-Chave:");
		horizontalPanel_4.add(lblPass1);
		lblPass1.setStyleName("label");

		final PasswordTextBox txtPass1 = new PasswordTextBox();
		horizontalPanel_4.add(txtPass1);
		txtPass1.setWidth("250");
		txtPass1.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_5);
		horizontalPanel_5.setSpacing(2);

		final Label lblPass2 = new Label("Confirmar Palavra-Chave:");
		horizontalPanel_5.add(lblPass2);
		lblPass2.setStyleName("label");

		final PasswordTextBox txtPass2 = new PasswordTextBox();
		horizontalPanel_5.add(txtPass2);
		txtPass2.setWidth("250");
		txtPass2.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_7 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_7);
		horizontalPanel_7.setSpacing(4);
	
		final HTML htmlAlerta = new HTML("New <i>HTML</i> panel");
		
		final Button btnRegistar = new Button();
		horizontalPanel_7.add(btnRegistar);
		btnRegistar.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				
				boolean alerta = false;
				int c = 0;
								
				if(txtPass1.getText().equals("") || txtPass2.getText().equals("") || txtPass3.getText().equals("") )
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» tem de introduzir as palavras-chave!<br>");
				}
				else if (!txtPass1.getText().equals(txtPass2.getText()))
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» a nova palavra-chave e a confirmação não são iguais!<br>");
				}				
				
				if(!alerta)
				{
					htmlAlerta.setHTML("a processar dados...");
					ServiceLogin.Util.getInstance().editaccount(Default.getSession("Username").toString(),txtPass1.getText(),txtPass3.getText(), new MyAsyncCallback(htmlAlerta));
				}
			}
		});
		btnRegistar.setStyleName("button");
		btnRegistar.setText("CONFIRMAR");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);
		horizontalPanel_2.setSpacing(2);


		horizontalPanel_2.add(htmlAlerta);
		htmlAlerta.setText("");
		htmlAlerta.setStyleName("erro");

	}

	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call.<br>
	 * <br>
	 * This class is the implementation of AsyncCallback for the edit
	 * account service
	 * 
	 * @author Amigos do Noddy
	 */
	static class MyAsyncCallback implements AsyncCallback
	{
		HTML html;
	
		/**
		 * Class constructor
		 * 
		 * @param html Widget that can contain arbitrary HTML
		 */
		public MyAsyncCallback(HTML html)
		{
			this.html= html;
		}
		  
		/**
		 * Success handler
		 */
		public void onSuccess(Object object)
		{
			Boolean success = (Boolean)object;
			String message = "";
			  
			if (success.booleanValue())
				message = "Alteração confirmada!";
			else
				message = "Palavra-Chave antiga incorrecta!";
				

			DOM.setInnerHTML(html.getElement(), message);
		}
		
		/**
		 * Failure handler
		 */
		public void onFailure(Throwable throwable) {
			System.out.println("FALHOU!");
		}
	}
}
